﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.Sellers;

namespace ZBJF.Warranty.BLL
{
    public partial class SellerInsuranceProductMappingsService
    {
        public SellerInsuranceProductMappings LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public bool AddSellerInsuranceProductMappings(long sellerInsuranceProductID, List<SellerInsuranceProductMappings> sellerInsuranceProductMappings)
        {
            var result = true;
            var sellerInsuranceProductMappingsIDs = new List<long>();
            if (sellerInsuranceProductID > 0 && sellerInsuranceProductMappings != null && sellerInsuranceProductMappings.Any())
            {
                sellerInsuranceProductMappings = sellerInsuranceProductMappings.Distinct().ToList();
                for (int i = 0; i < sellerInsuranceProductMappings.Count; i++)
                {
                    sellerInsuranceProductMappings[i].SellerInsuranceProductId = sellerInsuranceProductID;
                    if (!IsExist(sellerInsuranceProductMappings[i]))
                    {
                        sellerInsuranceProductMappings[i].SellerInsuranceProductId = sellerInsuranceProductID;
                        var id = AddEntities(sellerInsuranceProductMappings[i]).ID;
                        sellerInsuranceProductMappingsIDs.Add(id);
                        result = id > 0;
                    }
                    if (sellerInsuranceProductMappings[i].ID > 0)
                        sellerInsuranceProductMappingsIDs.Add(sellerInsuranceProductMappings[i].ID);
                }
            }
            var tobeDeleted = LoadEntities(o =>
                        o.SellerInsuranceProductId == sellerInsuranceProductID && !sellerInsuranceProductMappingsIDs.Contains(o.ID)).ToList();
            if (tobeDeleted.Any())
                tobeDeleted.ForEach(o => DeleteEntities(o));
            return result;
        }


        public bool IsExist(SellerInsuranceProductMappings entity)
        {
            var expressionWhere = PredicateExtensions.True<SellerInsuranceProductMappings>().And(o => o.SellerInsuranceProductId == entity.SellerInsuranceProductId);
            if (entity.ID > 0)//不能修改为不等于
                expressionWhere = expressionWhere.And(o => o.ID == entity.ID);
            if (entity.InsuranceProductId.HasValue)
                expressionWhere = expressionWhere.And(o => o.InsuranceProductId == entity.InsuranceProductId);
            if (entity.InsuranceProductPackageID.HasValue)
                expressionWhere = expressionWhere.And(o => o.InsuranceProductPackageID == entity.InsuranceProductPackageID);
            return LoadEntities(expressionWhere).AsNoTracking().Any();
        }


        public IQueryable<SellerInsuranceProductMappings> LoadInsuranceProducts(long sellerInsuranceProductsId)
        {
            return LoadEntities(o => o.SellerInsuranceProductId == sellerInsuranceProductsId).OrderBy(o => o.ID);
        }
        public List<int> GetAllInsuranceProductIDs(long sellerInsuranceProductID)
        {
            return DbSession.SellerInsuranceProductMappingsRepository.GetAllInsuranceProductIDs(sellerInsuranceProductID);
        }

        /// <summary>
        /// 获取商家产品映射的打包产品对应的基础产品
        /// </summary>
        /// <param name="sellerInsuranceProductID"></param>
        /// <returns></returns>
        public List<int> GetPackageInsuranceProductIDs(long sellerInsuranceProductID)
        {
            return DbSession.SellerInsuranceProductMappingsRepository.GetPackageInsuranceProductIDs(sellerInsuranceProductID);
        }
        /// <summary>
        /// 获取商家产品映射的基础产品
        /// </summary>
        /// <param name="sellerInsuranceProductID"></param>
        /// <returns></returns>
        public List<int> GetNotPackageInsuranceProductIDs(long sellerInsuranceProductID)
        {
            return LoadEntities(o => o.SellerInsuranceProductId == sellerInsuranceProductID && o.InsuranceProductId.HasValue &&
                    o.InsuranceProductId.Value > 0).Select(o => o.InsuranceProductId.Value).ToList();
        }
    }
}
